$(function () {
    if (window.parent === window.self) {
        max_width = window.document.documentElement.clientWidth;
        max_height = window.document.documentElement.clientHeight;
    } else {
        var panel = $(".tabs-panels", window.parent.document);

        max_width = panel.width;
        max_height = panel.height;
    }

    $('#listGrid').datagrid({
        toolbar: '#toolbar',
        title: '期间列表',
        iconCls: 'icon icon-chart_organisation',
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        url: '/sys/period/list',
        queryParams: {
            _token: $('meta[name="csrf-token"]').attr('content'),
            curr_menu_id: $('meta[name="curr_menu_id"]').attr('content')
        },
        columns: [[
            {title: 'id', field: 'id', hidden: true},
            {title: '名称', field: 'name', width: 130},
            {title: '是否生效', field: 'is_active', width: 130},
            {title: '开始日期', field: 'startdate', width: 130,formatter:function (value,row,index) {
                return value.substr(0,10);
            }},
            {title: '结束日期', field: 'enddate', width: 130,formatter:function (value,row,index) {
                return value.substr(0,10);
            }},
            {title: '是否打开', field: 'is_open', width: 130}
        ]],
        onLoadSuccess: function (data) {
            if (data === null || data.total === 0) {
                var body = $(this).data().datagrid.dc.body2;
                body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 35px; text-align: center;"><h1>暂无数据</h1></td></tr>');
            }
        }
    });
})

function openAdd() {
    $('#form_dialog').dialog({
        title: '添加信息',
        width: 400,
        height: 200,
        closed: false,
        cache: false,
        href: '/sys/period/add',
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function openEdit() {
    var rows = $('#listGrid').datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行", "info");
        return;
    }
    $('#form_dialog').dialog({
        title: '编辑信息',
        width: 400,
        height: 200,
        closed: false,
        cache: false,
        href: '/sys/period/edit?period_id=' + rows.id,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function save() {
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url: '/sys/period/save',
        type: 'post',
        data: $('#info-form').serialize(),
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $('#listGrid').datagrid("reload");
                $('#form_dialog').dialog('close');
            } else {
                $.messager.alert("提示信息", data.data, "error");
            }
        }
    });
}

function reload() {
    $('#listGrid').datagrid("reload");
}
function onOpen() {
    return open_operate(1);
}

function onClose() {
    return open_operate(0);
}
function open_operate(is_open) {
    var rows = $('#listGrid').datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行", "info");
        return;
    }
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url: '/sys/period/openorclose',
        type: 'post',
        data: {
            id:rows.id,
            is_open:is_open
        },
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $('#listGrid').datagrid("reload");
                $('#form_dialog').dialog('close');
            } else {
                $.messager.alert("提示信息", data.data, "error");
            }
        }
    });
}